package com.blast.comicsisland.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.BookstoreGameAdapter;
import com.blast.comicsisland.model.game.gamebody.GameBody;
import com.blast.comicsisland.model.myview.MyGridView;
import com.blast.comicsisland.model.rank.rankhead.Info;
import com.blast.comicsisland.model.recommend.recommendhead.AdListJson;
import com.blast.comicsisland.utils.ImageLoaderUtil;
import com.blast.comicsisland.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 为BookStroeFragment的子Fragment 游戏
 * 上方为ImageView 与TextView 二者帧布局 TextView在上层
 * 下方为GridView展示选项条目 不做
 */
public class GameFragment extends Fragment {


    private View view;
    private Info info;
    private NetworkImageView networkImageView;
    private TextView textView;
    private MyGridView gridView;
    private ImageLoader imageLoader;
    private ScrollView scrollView;

    public GameFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageLoaderUtil imageLoaderUtil = ImageLoaderUtil.getInstance();
        imageLoader = imageLoaderUtil.getImageLoader();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_game, container, false);
        networkImageView = (NetworkImageView) view.findViewById(R.id.bookstore_game_image_view);
        textView = (TextView) view.findViewById(R.id.bookstore_game_text_view);
        gridView = (MyGridView) view.findViewById(R.id.bookstore_game_grid_view);
        scrollView = (ScrollView) view.findViewById(R.id.bookstore_game_scrollview);
        scrollView.setFocusable(true);

        view.setVisibility(View.INVISIBLE);
        String headPath = "http://mhjk.1391.com/comic/getproad?adgroupid=33&e=" + (System.currentTimeMillis() / 1000) + "&platformtype=1&channelid=baidu";
        String bodyPath = "http://mhjk.1391.com/comic/getapplicationlist?pageno=1&pagesize=20&sort=1&e=" + (System.currentTimeMillis() / 1000) + "&grouptype=0&apptype=1&channelid=baidu";

        GameAsyncTask gameAsyncTask = new GameAsyncTask();
        gameAsyncTask.execute(headPath);
        GameAsyncTask gameAsyncTask1 = new GameAsyncTask();
        gameAsyncTask1.execute(bodyPath);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    class GameAsyncTask extends AsyncTask<String, Void, String> {

        private GameBody gameBody;

        @Override
        protected String doInBackground(String... params) {
            String ret = null;
            String path = params[0];
            if (path != null) {
                String jsonStr = JsonUtils.doGetRequest(path);
                ret = JsonUtils.stringToRegex(jsonStr);
            }

            return ret;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONArray jsonArray = object.optJSONArray("info");
                    JSONObject infoObject = object.optJSONObject("info");
                    if (infoObject != null) {
                        info = new Info();
                        info.jsonParse(infoObject);

                        List<AdListJson> adlistjson = info.getAdlistjson();
                        if (adlistjson != null) {
                            AdListJson adListJson = adlistjson.get(0);
                            if (adListJson != null) {
                                String imageurl = adListJson.getImageurl();
                                String title = adListJson.getTitle();
                                networkImageView.setDefaultImageResId(R.drawable.loading_land);
                                networkImageView.setImageUrl(imageurl, imageLoader);

                                textView.setText(title);
                            }

                        }


                    } else if (jsonArray != null) {
                        gameBody = new GameBody();
                        gameBody.jsonParse(jsonArray);

                        List<com.blast.comicsisland.model.game.gamebody.Info> infos = gameBody.getInfo();
                        if (infos != null) {
                            BookstoreGameAdapter gameAdapter = new BookstoreGameAdapter(getActivity(), infos);

                            gridView.setAdapter(gameAdapter);
                            view.setVisibility(View.VISIBLE);
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}
